package com.xl.yygh.hosp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xl.yygh.common.result.Result;
import com.xl.yygh.common.utils.MD5;
import com.xl.yygh.hosp.service.HospitalSetService;
import com.xl.yygh.model.model.hosp.HospitalSet;
import com.xl.yygh.model.vo.hosp.HospitalSetQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Random;

/**
 * @author XiangLiang
 * @date 2023/10/21 18:59
 * @description 医院管理Controller
 */
@Api(tags = "医院设置管理")
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
public class HospitalSetController {
    @Resource
    private HospitalSetService hospitalSetService;

    /**
     * 查询医院设置表所有信息
     * @return 医院设置表所有信息List
     */
    @ApiOperation(value = "获取所有医院设置")
    @GetMapping("findAll")
    public Result findAllHospitalSet() {
        List<HospitalSet> list = hospitalSetService.list();
        return Result.ok(list);
    }

    /**
     * 逻辑删除医院设置
     * @param id 节点ID
     * @return 逻辑删除医院设置结果
     */
    @ApiOperation(value = "逻辑删除医院设置")
    @DeleteMapping("{id}")
    public Result removeHospitalSet(@PathVariable String id) {
        boolean flag = hospitalSetService.removeById(id);
        if (flag) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    /**
     * 条件查询带分页
     * @param current 当前页
     * @param limit 每页记录数
     * @param hospitalSetQueryVo 查询VO
     * @return 条件查询带分页List
     */
    @ApiOperation(value = "条件查询带分页")
    @PostMapping("findPage/{current}/{limit}")
    public Result findHospSetPage(@PathVariable Long current,
                                  @PathVariable Long limit,
                                  @RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo) {
        Page<HospitalSet> page = new Page<>(current, limit);
        String hosName = hospitalSetQueryVo.getHosname();
        String hosCode = hospitalSetQueryVo.getHoscode();
        QueryWrapper<HospitalSet> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(!StringUtils.isEmpty(hosName), "hosname", hosName)
                .eq(!StringUtils.isEmpty(hosCode), "hoscode", hosCode);
        Page<HospitalSet> pageHospitalSet = hospitalSetService.page(page, queryWrapper);
        return Result.ok(pageHospitalSet);
    }

    /**
     * 添加医院设置
     * @param hospitalSet 医院设置实体
     * @return 添加结果
     */
    @ApiOperation(value = "添加医院设置")
    @PostMapping("saveHospitalSet")
    public Result saveHospitalSet(@RequestBody HospitalSet hospitalSet) {
        // 医院上线状态： 1（可用） / 2（不可用）,默认启用
        hospitalSet.setStatus(1);
        // 设置医院签名
        String signKey = MD5.encrypt(System.currentTimeMillis() + new Random().nextInt(1000) + "");
        hospitalSet.setSignKey(signKey);
        boolean isSave = hospitalSetService.save(hospitalSet);
        if (isSave) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    /**
     * 根据id获取医院设置
     * @param id 结点ID
     * @return 医院设置实体
     */
    @ApiOperation(value = "根据id获取医院设置")
    @GetMapping("getHospitalSet/{id}")
    public Result getHospitalSet(@PathVariable Long id) {
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        return Result.ok(hospitalSet);
    }

    /**
     * 修改医院设置
     * @param hospitalSet 医院设置实体
     * @return 修改结果
     */
    @ApiOperation(value = "修改医院设置")
    @PostMapping("updateHospitalSet")
    public Result updateHospitalSet(@RequestBody HospitalSet hospitalSet) {
        boolean isUpdate = hospitalSetService.updateById(hospitalSet);
        if (isUpdate) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    /**
     * 批量删除医院设置
     * @param ids 批量删除的idList
     * @return 删除结果
     */
    @ApiOperation(value = "批量删除医院设置")
    @DeleteMapping("batchRemove")
    public Result batchRemove(@RequestBody List<Long> ids) {
        hospitalSetService.removeByIds(ids);
        return Result.ok();
    }

    /**
     * 医院设置锁定和解锁
     * @param id 记录ID
     * @param status 医院设置状态
     * @return 锁定结果
     */
    @ApiOperation(value = "医院设置锁定和解锁")
    @PutMapping("lockHospitalSet/{id}/{status}")
    public Result lockHospitalSet(@PathVariable Long id,
                                  @PathVariable Integer status) {
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        hospitalSet.setStatus(status);
        hospitalSetService.updateById(hospitalSet);
        return Result.ok();
    }

    /**
     * 发送签名秘钥
     * @param id 记录ID
     * @return 发送签名结果
     */
    @ApiOperation(value = "发送签名秘钥")
    @PutMapping("sendSignKey/{id}")
    public Result sendSignKey(@PathVariable Long id) {
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        String hosCode = hospitalSet.getHoscode();
        String signKey = hospitalSet.getSignKey();
        // TODO 发送短信
        return Result.ok();
    }

}
